package utils

import (
	"math/rand"
	"os"
	"time"

	"github.com/spf13/viper"
	"gitlab-tech.shruisong.cn/growth/go-common/pkg/xjson"
)

func GetRandomString(l int) string {
	str := "0123456789abcdefghijklmnopqrstuvwxyz"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < l; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}

func IsJson(b []byte) bool {
	return xjson.Valid(b)
}

func Env() string {
	return viper.GetString("env")
}

func IsProduction() bool {
	return Env() == "prd"
}

func IsLocalDev() bool {
	return Env() == "local"
}

func IsQa() bool {
	return Env() == "qa"
}

// IsCli cli模式
func IsCli() bool {
	for _, v := range os.Args {
		if v == "api" {
			return false
		}
	}

	return true
}

func RandInt64(min, max int64) int64 {
	if min >= max || min == 0 || max == 0 {
		return max
	}
	rand.Seed(time.Now().UnixNano())
	return rand.Int63n(max-min) + min
}
